import { createConnection } from 'typeorm';
import { DB_CONNECTION_TOKEN } from '../../const';
import { ConfigService } from '../config/config.service';

export const databaseProviders = [
  {
    provide: DB_CONNECTION_TOKEN,
    useFactory: async config => {
      const { nodeEnv, dbHost, dbPort, dbUser, dbPassword, dbName } = config;
      return await createConnection({
        type: 'mysql',
        host: dbHost,
        port: dbPort,
        username: dbUser,
        password: dbPassword,
        database: dbName,
        entities: [
          __dirname + '/../**/*.entity{.ts,.js}',
        ],
        logging: ['error'],
        synchronize: true,
      });
    },
    inject: [ConfigService],
  },
];
